package org.zhq.frames;

import org.zhq.core.Frame;
import org.zhq.core.IoArgs;

import java.io.IOException;

/**
 * @author zhengquan
 * @date 2019/2/9
 */
public abstract class AbsReceiveFrame extends Frame {

    //帧体可读区域大小
    volatile int bodyRemaining;


    AbsReceiveFrame(byte[] header) {
        super(header);
        this.bodyRemaining = getBodyLength();
    }

    @Override
    public boolean handle(IoArgs args) throws IOException {
        if (bodyRemaining == 0) {
            return true;
        }
        bodyRemaining -= consumeBody(args);
        return bodyRemaining == 0;
    }

    @Override
    public Frame nextFrame() {
        return null;
    }

    @Override
    public int getConsumableLength() {
        return bodyRemaining;
    }

    protected abstract int consumeBody(IoArgs args) throws IOException;
}
